import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import 'package:myflutter/television/pages/index.dart';
import 'package:myflutter/television/signal.dart';

void main() {
  // 安卓电视软件
  runApp(MaterialApp(home: TVWidget(),));
}

class TVWidget extends StatefulWidget {
  TVWidget({ super.key});
  @override
  State<StatefulWidget> createState() {
    return TVWidgetState();
  }
}

typedef void onFocusChange(bool hasFocus);
typedef void onClick();
var _buttonName = "请按遥控器按钮";
class TVWidgetState extends State<TVWidget> {
  FocusNode _focusNode = FocusNode();
  PageShower _pageShower =  PageShower();


  var default_decoration = BoxDecoration(
      border: Border.all(width: 2, color: Colors.deepOrange),
      borderRadius: BorderRadius.all(
        Radius.circular(5),
      ));
  var decoration = null;


  @override
  void initState() {
    super.initState();
    _focusNode.requestFocus();
  }

  @override
  void dispose() {
    _focusNode.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {

    return RawKeyboardListener(
        focusNode: FocusNode(),
        autofocus: true, // 要开启自动焦点
        onKey: (RawKeyEvent event) {
          if (event is RawKeyDownEvent) {
            // 监听按下RawKeyDownEvent，松开RawKeyUpEvent，按下时候触发
              RawKeyEventDataAndroid rawKeyEventDataAndroid =
              event.data as RawKeyEventDataAndroid;
              // print(rawKeyEventDataAndroid.keyCode);
              switch (rawKeyEventDataAndroid.keyCode) {
                case 19:
                  print("上键");
                  _buttonName = "上键";
                  FocusScope.of(context).previousFocus();
                  _pageShower.signalRead(Signal.up);
                  break;
                case 20:
                  print("下键");
                  _buttonName = "下键";
                  FocusScope.of(context).nextFocus();
                  _pageShower.signalRead(Signal.down);
                  break;
                case 21:
                  print("左键");
                  _buttonName = "左键";
                  FocusScope.of(context).previousFocus();
                  _pageShower.signalRead(Signal.left);
                  break;
                case 22:
                  print("右键");
                  _buttonName = "右键";
                  FocusScope.of(context).nextFocus();
                  _pageShower.signalRead(Signal.right);
                  break;
                case 23:
                  print("中键");
                  _buttonName = "中键";
                  break;
                default:
                  print("其他");
                  _buttonName = "${rawKeyEventDataAndroid.keyCode}";
                  break;
              }
              setState(() {

              });
            }
        },
        child: Container(
          child: Center(
            child: ListView(children: [
              Text(_buttonName, style: TextStyle(color: Colors.white, fontSize: 100),),
              Text(_buttonName, style: TextStyle(color: Colors.white, fontSize: 100),),
              Text(_buttonName, style: TextStyle(color: Colors.white, fontSize: 100),),
              Text(_buttonName, style: TextStyle(color: Colors.white, fontSize: 100),),
              Text(_buttonName, style: TextStyle(color: Colors.white, fontSize: 100),),
              Text(_buttonName, style: TextStyle(color: Colors.white, fontSize: 100),),
            ],)
            // child: _pageShower
            // child: Text(_buttonName, style: TextStyle(color: Colors.white, fontSize: 100),),
          ),
        ));

  }
}